package com.ld.security.server.admin.modules.ge.vo;

import com.ld.security.common.core.base.SuperPo;
import com.ld.security.common.core.util.JsonUtil;
import com.ld.security.common.core.util.MapUtil;
import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.mybatis.anno.QueryKey;
import com.ld.security.common.mybatis.anno.SqlKey;
import com.ld.security.common.mybatis.anno.SqlKeys;
import com.ld.security.common.mybatis.base.vo.ProxyVo;
import com.ld.security.common.mybatis.constant.SqlCondition;
import com.ld.security.server.admin.modules.ge.constant.GeCompConstant;
import com.ld.security.server.admin.modules.ge.entity.*;
import lombok.Data;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 详情界面下拉字段用
 */
@Data
public class GeModelArgsVo implements SuperPo,ProxyVo {

    private String dataSourceId;

    private String viewId;

    private String projectId;


    private GeCompConstant.compScope compScope = GeCompConstant.compScope.global;




    @QueryKey
    @SqlKey(name = "dataSourceId",valueField = "dataSourceId")
    private List<GeTableEntity> tables;


    @QueryKey
    @SqlKey(name = "tableId",valueField = "tables.id",value = SqlCondition.in)
    private List<GeColumnEntity> columns;

    @QueryKey
    @SqlKeys(value = {
            @SqlKey(name = "viewId",valueField = "viewId",value = SqlCondition.eq),
            @SqlKey(name = "compScope",valueField = "compScope",value = SqlCondition.eq,and = false),
            @SqlKey(name = "projectId",valueField = "projectId",value = SqlCondition.eq,and = false),
    })
    private List<GeCompEntity> comps;

    @QueryKey
    @SqlKey(name = "viewId",valueField = "viewId",value = SqlCondition.eq)
    private List<GeFuncEntity> funcList;


    private List<GeFieldColumnVo> fieldColumns;

    @Override
    public void queryAfter() {
        this.fieldColumns=new LinkedList<>();
        Map<String,GeTableEntity> tableMap =  MapUtil.objToMap(tables,"id");
        for (GeColumnEntity column : columns) {
            GeTableEntity tableEntity =  tableMap.get(column.getTableId());

            GeFieldColumnVo e =  JsonUtil.clone(column,GeFieldColumnVo.class);
            String columnName = tableEntity.getTableName()+"."+column.getColumnName();
            String label=tableEntity.getLabel()+"-"+column.getLabel();
            e.setColumnName(columnName);
            e.setLabel(label);

            GeFieldEntity field = new GeFieldEntity();



            //field.setFieldType();
            field.setLabel(column.getLabel());
            field.setColumnName(columnName);
            field.setFieldName(StringUtil.columnToJava(column.getColumnName()));
            e.setField(field);
            field.setTableName(column.getTableName());
            field.setGroupCode(field.getFieldName());
            ReflectionUtil.fill(field);
            field.saveBefore();
            fieldColumns.add(e);
        }




        this.columns=null;
    }
}
